/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "ProjectResourceManager.h"

namespace PQEngine
{
	const std::string ProjectDefaultResource::BRUSH_PATH="./brushes";
	const std::string ProjectDefaultResource::BRUSH_GROUP="BrushGroup";

	const std::string ProjectResourceGroupType::FILE_SYSTEM="FileSystem";
	const std::string ProjectResourceGroupType::ZIP="Zip";
	ProjectResourceGroup::ProjectResourceGroup()
	{
		_count=0;
	}

	ProjectResourceGroup& ProjectResourceGroup::getSingleton()
	{
		static ProjectResourceGroup inst;
		return inst;
	}

	void ProjectResourceGroup::getResourceList()
	{
		//search ogre archive system
		Ogre::FileInfoListPtr list = Ogre::ResourceGroupManager::getSingleton().listResourceFileInfo("Brushes");
		Ogre::FileInfoList::const_iterator it = list->begin();
		for(; it != list->end(); ++it){
			Ogre::FileInfo info = (*it);
			if(info.archive->getType() == "FileSystem"){
				if(info.filename.find(".png") == -1) continue;
				Ogre::String filename = info.archive->getName() + "/";
				filename += info.filename;
			}
		}
	}

	bool ProjectResourceGroup::addResource(std::string path)
	{
		if(!isLegalResource(path)){
			return false;
		}
		if(!hasResourceLoc(path)){
			std::string groupName=getResourceGroupName(path);
			Ogre::ResourceGroupManager::getSingleton().addResourceLocation(path,ProjectResourceGroupType::FILE_SYSTEM,groupName);
			Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(groupName);
			_folderPathes.push_back(path);
			_resourceGroupNames.push_back(groupName);
		}
		return true;
	}

	std::string ProjectResourceGroup::getResourceGroupName(std::string path)
	{
		int size=_resourceGroupNames.size();
		std::string groupName="ResourceGroup"+Ogre::StringConverter::toString(size);
		return groupName;
	}

	bool ProjectResourceGroup::hasResourceLoc(std::string path)
	{
		int size=_folderPathes.size();
		for(int i=0;i<size;i++){
			if(_folderPathes[i]==path){
				return true;
			}
		}
		return false;
	}
	
	bool ProjectResourceGroup::isLegalResource(std::string path)
	{
		return true;
	}

	ProjectResourceType ProjectResourceManager::getResourceExtType(std::string type)
	{
		if(type=="mesh"||type=="MESH"){
			return RES_TYPE_MESH;
		}
		if(type=="cs"||type=="CS"){
			return RES_TYPE_SCRIPT_CSHARP;
		}
		if(type=="blend"||type=="BLEND"){
			return RES_TYPE_FILE_BLEND;
		}
		return RES_TYPE_UNKNOWN;
	}

	ProjectResourceManager::ProjectResourceManager()
	{
	}

	ProjectResourceManager& ProjectResourceManager::getSingleton()
	{
		static ProjectResourceManager inst;
		return inst;
	}

	void ProjectResourceManager::loadDefaultResource()
	{
		
	}
}
